package com.example.action;

import com.example.bean.Page;
import com.example.bean.User;
import com.example.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/selectOrder")
public class SelectOrderServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pageNoStr = req.getParameter("pageNo");
        int pageNo = 1;

        if (pageNoStr != null && !pageNoStr.trim().isEmpty()) {
            try {
                pageNo = Integer.parseInt(pageNoStr);
            } catch (NumberFormatException e) {
                pageNo = 1;
            }
        }
        User user = (User) req.getSession().getAttribute("loginUser");
        OrderService service = new OrderService();
        int pageSize = 3;
        Page page = service.selectOrderByUid(user.getId(), pageNo, pageSize);
        req.setAttribute("page", page);
        req.setAttribute("orderList", page.getList());
        req.getRequestDispatcher("order_list.jsp").forward(req, resp);
    }
}